﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityClient;

namespace HelpCentral.Data.Repository
{
    public class BaseRepository<T> where T : EntityObject
    {
        protected IObjectSet<T> _objectSet;
        protected ObjectContext _context;

        public ObjectContext Context { get { return _context; } }
        public IObjectSet<T> ObjectSet { get { return _objectSet; } }

        public BaseRepository(ObjectContext context)
        {
            _context = context;
            _objectSet = context.CreateObjectSet<T>();
        }

        public IQueryable<T> Where(Func<T, bool> predicate)
        {
            return _objectSet.Where(predicate).AsQueryable();
        }

        public T FirstOrDefault()
        {
            return _objectSet.FirstOrDefault();
        }

        public T FirstOrDefault(Func<T, bool> predicate)
        {
            return _objectSet.FirstOrDefault(predicate);
        }

        public IQueryable<T> All()
        {
            return _objectSet.AsQueryable();
        }

        public void Add(T obj)
        {
            _objectSet.AddObject(obj);
        }
    }
}
